//
//  State Manager.m
//  GLSprite
//
//  Created by Daniel Polak on 7/15/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "State Manager.h"
#import "Mind.h"

#import "BaseState.h"
#import "SplashState.h"
#import "MenuState.h"
#import "TallyState.h"
#import "LoadingState.h"
#import "GameplayState.h"
#import "LevelSelectState.h"
#import "OptionsState.h"
#import "WinLoseState.h"
#import "RenderManager.h"

@implementation StateManager
@synthesize m_vStates, m_pCurrentState;
@synthesize m_nMyState;

static StateManager* instance = nil;

+ (StateManager *)stateManager { 
	if ( instance == nil ) { 
		instance = [[self alloc] init];
	} 
	return instance;
}

- (int)MyState
{
	return m_nMyState;
}

- (void)Init {
	m_vStates = [[Vector alloc] init];
}

- (void)PushState:(int)state {
	[m_pCurrentState Exit];
	
	switch (state) {
		case SPLASHSTATE:
		{
			SplashState* splashState = [[SplashState alloc] init];
			m_pCurrentState = splashState;
			m_nMyState = SPLASHSTATE;
			[m_vStates push:splashState];
			[splashState Enter];
			break;
		}
		case MENUSTATE:
		{
			MenuState* newState = [[MenuState alloc] init];
			m_pCurrentState = newState;
			m_nMyState = MENUSTATE;
			[m_vStates push:newState];
			[newState Enter];
			break;
		}
		case GAMEPLAYSTATE:
		{
			GameplayState* newState = [[GameplayState alloc] init];
			m_pCurrentState = newState;
			m_nMyState = GAMEPLAYSTATE;
			[m_vStates push:newState];
			[newState Enter];
			break;
		}
		case OPTIONSSTATE:
		{
			OptionsState* newState = [[OptionsState alloc] init];
			m_pCurrentState = newState;
			m_nMyState = OPTIONSSTATE;
			[m_vStates push:newState];
			[newState Enter];
			break;
		}
		case TALLYSTATE:
		{
			TallyState* newState = [[TallyState alloc] init];
			m_pCurrentState = newState;
			m_nMyState = TALLYSTATE;
			[m_vStates push:newState];
			[newState Enter];
			break;
		}
		case LEVELSELECTSTATE:
		{
			LevelSelectState* newState = [[LevelSelectState alloc] init];
			m_pCurrentState = newState;
			m_nMyState = LEVELSELECTSTATE;
			[m_vStates push:newState];
			[newState Enter];
			break;
		}
		case WINLOSESTATE:
		{
			WinLoseState* newState = [[WinLoseState alloc] init];
			m_pCurrentState = newState;
			m_nMyState = WINLOSESTATE;
			[m_vStates push:newState];
			[newState Enter];
			break;
		}
		case LOADINGSTATE:
		{
			LoadingState* newState = [[LoadingState alloc] init];
			m_pCurrentState = newState;
			m_nMyState = LOADINGSTATE;
			[m_vStates push:newState];
			[newState Enter];
			break;
		}
		default:
			break;
	}	
}

- (void)PopState {
	[m_pCurrentState Exit];
	[m_vStates pop];
	m_pCurrentState = (BaseState*)([[m_vStates vector] objectAtIndex:0]);
}

- (void)SetState:(BaseState*)newState {
	
}

- (void)Update {
	[m_pCurrentState Update];
	[[RenderManager renderManager] UpdateParticles];
}

- (void)Render {
	[m_pCurrentState Render];
}

@end
